import type { IColumnsOption } from 'vc';
import { CellRenderMap, toF9Page, INDEX, setHrefUrlTool } from '@/utils';

export function useTable(traceSource: any) {
  const totalNum = ref(0);

  const tableData = ref<any[]>([]);

  const columns = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      title: '银行名称',
      field: 'companyShortName',
      fixed: 'left',
      width: 200,
      cellRender: {
        name: CellRenderMap.MyCompany,
        events: { click: toF9Page }
      }
    },
    {
      title: '数据年度',
      field: 'statisticalYear',
      align: 'center'
    },
    {
      title: '报告期',
      field: 'reportingPeriodTypeCode',
      align: 'center'
    },
    {
      title: '值类型',
      field: 'valueTypeCode',
      align: 'center'
    },
    {
      title: '支持的项目数量',
      field: 'numberOfSupportedProjects',
      align: 'center',
      sortable: true,
      sortBy: 'numberOfSupportedProjects',
      slots: {
        default: ({ row }) => {
          let showValue = '';
          if (row?.numberOfSupportedProjects === 0) {
            showValue = '0个';
          } else {
            showValue = row?.numberOfSupportedProjects ? row.numberOfSupportedProjects + '个' : '-';
          }
          if (traceSource.value) {
            return <a onClick={() => setHrefUrlTool(row.numberOfSupportedProjectsObj.fileType, row.numberOfSupportedProjectsObj.fileUrl)}>{showValue}</a>;
          } else {
            return showValue;
          }
        }
      }
    },
    {
      title: '贷款金额',
      field: 'loanAmount',
      align: 'center',
      sortable: true,
      sortBy: 'loanAmount',
      slots: {
        default: ({ row }) => {
          let showValue = '';
          if (row.loanAmount == 0) {
            showValue = '0亿元';
          } else {
            showValue = row.loanAmount ? row.loanAmount + '亿元' : '-';
          }
          if (traceSource.value) {
            return <a onClick={() => setHrefUrlTool(row.loanAmountObj.fileType, row.loanAmountObj.fileUrl)}>{showValue}</a>;
          } else {
            return showValue;
          }
        }
      }
    },
    {
      title: '加权平均率',
      field: 'interestRate',
      align: 'center',
      sortable: true,
      sortBy: 'interestRate',
      slots: {
        default: ({ row }) => {
          let showValue = '';
          if (row.interestRate == 0) {
            showValue = '0%';
          } else {
            showValue = row.interestRate ? row.interestRate + '%' : '-';
          }
          if (traceSource.value) {
            return <a onClick={() => setHrefUrlTool(row.interestRateObj.fileType, row.interestRateObj.fileUrl)}>{showValue}</a>;
          } else {
            return showValue;
          }
        }
      }
    },
    {
      title: '带动年度碳减排量',
      field: 'annualCERs',
      align: 'center',
      sortable: true,
      sortBy: 'annualCERs',
      slots: {
        default: ({ row }) => {
          let showValue = '';
          if (row.annualCERs == 0) {
            showValue = '0万吨二氧化碳';
          } else {
            showValue = row.annualCERs ? row.annualCERs + '万吨二氧化碳' : '-';
          }
          if (traceSource.value) {
            return <a onClick={() => setHrefUrlTool(row.annualCERsObj.fileType, row.annualCERsObj.fileUrl)}>{showValue}</a>;
          } else {
            return showValue;
          }
        }
      }
    }
  ]);

  return {
    totalNum,
    tableData,
    columns
  };
}
